package efr.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import efr.dao.AutorDao;
import efr.dao.PoezieDao;
import efr.dao.impl.AutorDaoImpl;
import efr.dao.impl.PoezieDaoImpl;
import efr.entity.Autor;
import efr.entity.Poezie;
import efr.util.EfrAdmUtil;

/**
 * Servlet implementation class Op4PoezieSubmitServlet
 */
public class Op4PoezieSubmitServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(Op4PoezieSubmitServlet.class);
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Op4PoezieSubmitServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		logger.debug("start doGet()");
		if("Modifica".equals(request.getParameter("action"))){
			String id = request.getParameter("id");
			String titlu = request.getParameter("titlu");
			String idAutor = request.getParameter("autor");
			String versuri = request.getParameter("versuri");
			String date = request.getParameter("date");
			String poezieDefault = request.getParameter("poezieDefault");
			String numeFile = request.getParameter("numeFile");
			String idYtb = request.getParameter("idYtb");
			
			Poezie poezie = new Poezie();
			poezie.setId(Integer.valueOf(id));
			poezie.setTitlu(titlu);
			poezie.setVersuri(versuri);
			poezie.setIdYtb(idYtb);
			poezie.setNumeFile(numeFile);
			poezie.setPoezieDefault(Integer.valueOf(poezieDefault));
			poezie.setDate(EfrAdmUtil.convertStringToDate(date, "yyyy-MM-dd HH:mm:ss.S"));//2013-07-07 17:55:32.0
			
			AutorDao autorDao = new AutorDaoImpl();
			Autor autor = autorDao.getAutor(Integer.valueOf(idAutor));	
			poezie.setAutor(autor);
			
			PoezieDao poezieDao = new PoezieDaoImpl();
			if (poezie.getPoezieDefault() == 1) {
				Poezie pDefault = poezieDao.getPoezieDefault();
				pDefault.setPoezieDefault(0);
				poezieDao.updatedPoezie(pDefault);				
				poezieDao.updatedPoezie(poezie);
				logger.debug("pDefault: "+pDefault);
				logger.debug("poezie: "+poezie);
			}else{
				poezieDao.updatedPoezie(poezie);
			}
			
		}
		
		if("Adauga".equals(request.getParameter("action"))){
			String titlu = request.getParameter("titlu");
			String idAutor = request.getParameter("autor");
			String versuri = request.getParameter("versuri");
			String date = request.getParameter("date");
			String poezieDefault = request.getParameter("poezieDefault");
			String numeFile = request.getParameter("numeFile");
			String idYtb = request.getParameter("idYtb");
			
			Poezie poezie = new Poezie();
			poezie.setTitlu(titlu);
			poezie.setVersuri(versuri);
			poezie.setIdYtb(idYtb);
			poezie.setNumeFile(numeFile);
			poezie.setPoezieDefault(Integer.valueOf(poezieDefault));
			poezie.setDate(EfrAdmUtil.convertStringToDate(date, "yyyy-MM-dd HH:mm:ss.S"));//2013-07-07 17:55:32.0
			
			AutorDao autorDao = new AutorDaoImpl();
			Autor autor = autorDao.getAutor(Integer.valueOf(idAutor));	
			poezie.setAutor(autor);
			
			PoezieDao poezieDao = new PoezieDaoImpl();
			poezieDao.addPoezie(poezie);
			
		}
		
		RequestDispatcher view = request.getRequestDispatcher("/ListPoezii.do");
		logger.debug("end doGet()");
		view.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
